package com.mdms.init;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import com.mdms.commands.BaseCommand;
import com.mdms.commands.user.GetUserRights;
import com.mdms.dto.MdmResources;
import com.mdms.dto.UdtPremits;
import com.mdms.interfaces.orm.CrudDAO;
import com.mdms.logger.SimplyMDLogger;

public class GetDBResourcesCommand extends BaseCommand{

	
	private static final long serialVersionUID = 1L;
	
	private  HashMap<String, HashMap<String, String>> resources;
	
	private final static SimplyMDLogger logger = new SimplyMDLogger(GetDBResourcesCommand.class);

	@Override
	public void execute() throws Exception {
		logger.debug("execute", "START");
		try{
		CrudDAO crudDAO = getDaoFactory().getCrudDAO();
		crudDAO.setPersistentClass(MdmResources.class);
		Transaction tx= crudDAO.beginTransaction();
		List <MdmResources>result = crudDAO.findAll();
		tx.commit();
		if(result!=null && result.size()>0){
			resources=new HashMap<String, HashMap<String, String>>();
			for (MdmResources mdmResources : result) {
				if(!resources.containsKey(mdmResources.getRes_lang())){
					HashMap<String,String> hashMaps = new HashMap<String,String>();
					resources.put(mdmResources.getRes_lang(), hashMaps);
				}
				HashMap<String, String> hashMaps = resources.get(mdmResources.getRes_lang());
				hashMaps.put(mdmResources.getRes_code(), mdmResources.getRes_value());	
			}
			
		}else{
			setErrorCode(-1);
			setErrorDesc("No database resources");
		}
		}catch(Exception e)	{
			setErrorCode(-1);
			setErrorDesc(e.getMessage());
			logger.error("execute", e);
		}
		logger.debug("execute", "START");
		
	}
	

	public HashMap<String, HashMap<String, String>> getResources() {
		return resources;
	}
	
}
